package org.jdcneter.cluster.jgroups;

import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdcenter.face.ClusterManager;
import org.jdcenter.face.PluginsFactory;
import org.jdcneter.cluster.Cluster;
import org.jdcneter.system.ServerConfig;

import org.jgroups.Address;
import org.jgroups.Channel;
import org.jgroups.ChannelListener;
import org.jgroups.ExtendedReceiverAdapter;
import org.jgroups.JChannel;

public class JGroupsImpl extends ExtendedReceiverAdapter implements JGroupsImplMBean,ChannelListener,Cluster{
	
	private Log log = LogFactory.getLog(getClass());

	private boolean startup = false;
	private int loadNum = 0;
	private String groupName = "defaultGroup";
	private String protocolURL = "protocol/udp.xml";
	private ClusterManager clusterManager;
	private boolean use_blocking = false;
	private String objName;
	
	private Channel channel;
	
	public JGroupsImpl(){
		
	}
	
	public JGroupsImpl(String groupName,boolean startup,int loadNum,String protocolURL,
			ClusterManagerJGroupsImpl clusterManager,boolean use_blocking){
		this.groupName = groupName;
		this.startup = startup;
		this.loadNum = loadNum;
		this.protocolURL = protocolURL;
		this.clusterManager = clusterManager;
		this.objName = "Cluster:clusterName="+groupName;
		this.use_blocking = use_blocking;
	}
	
	public void start() throws Exception{
		log.info("JGroups impl start");
		clusterManager.setCluster(this);
		try{
			PluginsFactory.getDefJMXInstance().registerMBean(objName, this);
		}catch(Exception e){
			log.error(e);
			throw e;
		}
		URL url = new URL(new URL(System.getProperty(ServerConfig.CONF_URL)),protocolURL);
		
		channel = new JChannel(url);
		channel.setOpt(Channel.BLOCK, use_blocking);
		channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
		channel.setReceiver(this);
		channel.addChannelListener(this);
		if(startup){
			channel.connect(groupName);
		}
		//loadNum
	}
	
	@Override
	public String getGroupName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getLoadNum() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setGroupName(String groupName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setLoadNum(int loadNum) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startCluster() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void stopCluster() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void channelClosed(Channel arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void channelConnected(Channel arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void channelDisconnected(Channel arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void channelReconnected(Address arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void channelShunned() {
		// TODO Auto-generated method stub
		
	}

}
